package mulegame;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.ComponentOrientation;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

/**
 * Creates the start screen with action listener 
 * 
 * @author AREA 29
 */

class StartScreen extends JPanel {
    
    private JComboBox comboBoxPlayers;
    private JComboBox comboBoxDiff;
    private JComboBox comboBoxMap;
	private GraphicsControl control;
	
	/**
	 * Makes start screen for difficulty level, map, and number of players. Drop Down menus for each of these 
	 * and action Listener
	 * 
	 * @param ctrl graphic controller object
	 */
	public StartScreen(GraphicsControl ctrl) {
	    control = ctrl;
		setSize(new Dimension(900, 600));
		setBorder(new MatteBorder(4, 4, 4, 4, (Color) new Color(0, 0, 0)));
		setLayout(null);
		
		JLabel lblMainTitle = new JLabel("Coffee Mule");
		lblMainTitle.setBounds(175, 11, 563, 106);
		lblMainTitle.setForeground(new Color(165, 42, 42));
		lblMainTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblMainTitle.setFont(new Font("Batang", Font.BOLD, 75));
		add(lblMainTitle);
		
		comboBoxDiff = new JComboBox();
		comboBoxDiff.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		comboBoxDiff.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxDiff.setFont(new Font("MV Boli", Font.BOLD, 42));
		comboBoxDiff.setModel(new DefaultComboBoxModel(new String[] {"Tall [Beginner]", "Grande [Standard]", "Venti [Tournament]"}));
		comboBoxDiff.setBounds(374, 229, 469, 60);
		comboBoxDiff.setToolTipText("Choose the Difficulty");
		add(comboBoxDiff);
		
		comboBoxPlayers = new JComboBox();
		comboBoxPlayers.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		comboBoxPlayers.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxPlayers.setFont(new Font("MV Boli", Font.BOLD, 42));
		comboBoxPlayers.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4"}));
		comboBoxPlayers.setBounds(769, 141, 74, 60);
		comboBoxPlayers.setToolTipText("Choose the Number of Players");
		add(comboBoxPlayers);
		
		comboBoxMap = new JComboBox();
		comboBoxMap.setToolTipText("Choose the Map");
		comboBoxMap.setFont(new Font("MV Boli", Font.BOLD, 42));
		comboBoxMap.setModel(new DefaultComboBoxModel(new String[] {"Standard", "Custom Map 1", "Custom Map 2", "Custom Map 3", "Custom Map 4", "Custom Map 5", "Random"}));
		comboBoxMap.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxMap.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		comboBoxMap.setBounds(428, 323, 415, 60);
		add(comboBoxMap);
		
		JLabel lblDiff = new JLabel("Level");
		lblDiff.setHorizontalAlignment(SwingConstants.LEFT);
		lblDiff.setForeground(new Color(0, 0, 0));
		lblDiff.setFont(new Font("MV Boli", Font.BOLD, 62));
		lblDiff.setBounds(42, 229, 313, 60);
		add(lblDiff);
		
		JLabel lblPlayers = new JLabel("Number of Players");
		lblPlayers.setHorizontalAlignment(SwingConstants.LEFT);
		lblPlayers.setForeground(Color.BLACK);
		lblPlayers.setFont(new Font("MV Boli", Font.BOLD, 62));
		lblPlayers.setBounds(42, 141, 696, 60);
		add(lblPlayers);

		JLabel lblMap = new JLabel("Select Map");
		lblMap.setHorizontalAlignment(SwingConstants.LEFT);
		lblMap.setForeground(Color.BLACK);
		lblMap.setFont(new Font("MV Boli", Font.BOLD, 62));
		lblMap.setBounds(42, 323, 376, 60);
		add(lblMap);
		
		JLabel lblLogoBIG = new JLabel("");
		lblLogoBIG.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		lblLogoBIG.setIcon(new ImageIcon("src/mulegame/Images/Logo.JPG"));
		lblLogoBIG.setBounds(187, 408, 185, 164);
		add(lblLogoBIG);
		
		JButton btnBegin = new JButton("Begin the Game");
		btnBegin.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				int numPlayers = comboBoxPlayers.getSelectedIndex() + 2;
				int diffIndex = comboBoxDiff.getSelectedIndex();
				GameConfig.Difficulty diff = null;
				switch(diffIndex){
				case(0):
				    diff = GameConfig.Difficulty.BEGINNER;
				    break;
				case(1):
				    diff = GameConfig.Difficulty.STANDARD;
				    break;
				case(2):
				    diff = GameConfig.Difficulty.TOURNAMENT;
				    break;
				}
				
				int mapType = comboBoxMap.getSelectedIndex();
				doClickSound(); 
				control.finishStartScreen(numPlayers, diff, mapType);
			}
		});
		btnBegin.setForeground(new Color(0, 0, 255));
		btnBegin.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnBegin.setFont(new Font("Times New Roman", Font.PLAIN, 35));
		btnBegin.setBounds(479, 408, 303, 68);
		add(btnBegin);
		
		JButton btnLoadGame = new JButton("Load Saved Game");
		btnLoadGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				control.loadGame();
				doClickSound(); 
			}
		});
		btnLoadGame.setForeground(Color.BLUE);
		btnLoadGame.setFont(new Font("Times New Roman", Font.PLAIN, 35));
		btnLoadGame.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnLoadGame.setBounds(479, 504, 303, 68);
		add(btnLoadGame);
	}
	
	/**
	 * plays audio clip for clicking action
	 */
    public void doClickSound() {
    	String soundName = "src/mulegame/Sounds/click.wav";    
    	AudioInputStream audioInputStream = null;
		try {
			audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	Clip clip = null;
		try {
			clip = AudioSystem.getClip();
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	try {
			clip.open(audioInputStream);
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	clip.start();
    }
}